#include <cmath>
#include <iostream>
using namespace std;
int fun[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
long long Fun(int x, int y, long long a)
{
	int temp, i;
	x--;
	y--;
	if (x != y)
	{
		temp = fun[x];
		fun[x] = fun[y];
		fun[y] = temp;
	}
	for (i = 0; i < 10; i++)
	{
		switch (fun[i])
		{
		case 1:
			a = a * 11 + 24;
			break;
		case 2:
			a = (a >> 3) + 3;
			break;
		case 3:
			a = a * a + 10 * a + 31;
			break;
		case 4:
			a = a / 13 + 31;
			break;
		case 5:
			a = a - (long long)sqrt(a);
			break;
		case 6:
			a = (long long)sqrt(a);
			break;
		case 7:
			a = a % 206211;
			break;
		case 8:
			a = a | 10311999;
			break;
		case 9:
			a = a & 19991124;
			break;
		case 10:
			a = a ^ 11241031;
			break;
		}
	}
	return a;
}
int main()
{
	int n, x, y;
	long long a;
	cin >> n;
	while (n--)
	{
		cin >> x >> y >> a;
		cout << Fun(x, y, a) << endl;
	}
	return 0;
}